package com.cyy.dao;

import com.cyy.bean.record;
import com.cyy.util.dbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public abstract class recordDao {
    public static void updateDataAfterModifyProduct() throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="update record,product set record.price=product.price where record.productName=product.name and record.price!=product.price";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        int i= pstmt.executeUpdate();
        dbUtil.close(null,pstmt,conn);
    }
    public static void updateNum(int recordId) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="update record set num=num+1 where recordId=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1,recordId);
        int i= pstmt.executeUpdate();
        dbUtil.close(null,pstmt,conn);
    }
    public static int getMaxid() throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select recordId from record";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet resultSet = pstmt.executeQuery();
        int index=0;
        while(resultSet.next())
        {
            if(resultSet.getInt("recordId")>index) {
                index = resultSet.getInt("recordId");
            }
        }
        return index;
    }
    public abstract int selectByProductAndUser(String userName,String productName) throws SQLException, ClassNotFoundException;
    public abstract void add(record r) throws SQLException, ClassNotFoundException;
    public abstract ArrayList<record> selectAll(String name,int type1) throws SQLException, ClassNotFoundException;

}
